package com.wingy.psyduckplayer.http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;

public class LrcFileDownload {
    public static final String LRC_SEARCH_URL = "http://geci.me/api/lyric/";
    private static final int REQUEST_TIMEOUT = 10*1000;
    private static final int SO_TIMEOUT = 10*1000;

    public static String getSongLRCUrl(String path, String songName)throws Exception {
        String url =null;
        String str_json = null;

        //歌词名为空返回Null;
        if(songName == null)    return null;

        String name = URLEncoder.encode(songName, "UTF-8");
        str_json = getHtmlCode(path + name);

        if(str_json == null) return null;

        JSONObject jsonObject = new JSONObject(str_json);
        int count = jsonObject.getInt("count");
        if(count == 0) return null;
        //获取歌词url列表，只取第一个
        JSONArray jsonArray= jsonObject.getJSONArray("result");
        JSONObject item = jsonArray.getJSONObject(0);
        url = item.getString("lrc");

        return url;
    }

    public static HttpClient getHttpClient() {
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        return httpClient;
    }

    public static String getHtmlCode(String path) {
        String result = null;
        try {
            HttpClient httpClient = new DefaultHttpClient(); //getHttpClient();
            HttpGet get = new HttpGet(path);
            HttpResponse response = httpClient.execute(get);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), "utf-8"));
                String line;
                result = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line + "\n";
                }
            }
        } catch (ConnectTimeoutException e) {
            System.out.println("ConnectTimeoutException timeout");
            return null;
        } catch (SocketTimeoutException e) {
            System.out.println("SocketTimeoutException timeout");
            return null;
        } catch (Exception e) {
            System.out.println(e.getMessage()+":"+e.toString());
        }
        return result;
    }


}
